package com.cash.app.ui.main.fg.account

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.cash.app.base.BaseViewModel
import com.cash.app.constant.AppConstant
import com.cash.app.manager.AccountInfoManager
import com.cash.app.manager.api.GlobalAPIService
import com.cash.app.manager.model.AccountDetailResponse
import com.cash.app.net.NetRequestExecutor
import com.cash.app.net.RetrofitClient
import com.cash.app.utils.SpUtils
import com.cash.app.utils.TLog

class AccountViewModel : BaseViewModel() {

    private val globalAPIService by lazy {
        RetrofitClient.createService(GlobalAPIService::class.java)
    }

    private val _accountDetail = MutableLiveData<AccountDetailResponse>()
    val accountDetail: LiveData<AccountDetailResponse> = _accountDetail

    private val _emptyInfo = MutableLiveData<Boolean>()
    val emptyInfo: LiveData<Boolean> = _emptyInfo

    fun getAccountDetail() {
        if (!SpUtils.get(AppConstant.K_APP_LOGIN, false)) {
            TLog.d("getAccountDetail need login", "AccountViewModel")
            _emptyInfo.value = true
            return
        }
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                globalAPIService.getAccountDetail()
            }
        }, onSuccess = { response ->
            response?.let {
                _emptyInfo.value = false
                _accountDetail.value = it
                AccountInfoManager.updateAccountInfo(it)
            }
        }, onError = { _, _ ->
            _emptyInfo.value = true
        })
    }
}